package Modes;

import java.util.ArrayList;
import java.util.List;

import Bacterias.HelmetBacteria;
import Bacterias.ShieldBacteria;
import android.graphics.Canvas;
import android.util.Log;
import android.view.MotionEvent;

import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.MinhasCoisas;
import com.game.ThauanFramework.TimeManager;
import com.thauanLopes.bacterium.GameSceneView;
import com.thauanLopes.bacterium.GameManages.GameManager;

public class ShieldBacteriaManager implements GameManager {

	private String TAG = "Shield Bacteria Manager";

	private float minInterval = 0.8f;
	private float maxInterval = 1.2f;
	private float nextInterval;

	private float intervalUpDificult = 10;
	private float difucultIncrease = 0.1f;
	private boolean upDificultOn = true;
	private int contadorDificuldade = 0;
	private int dificultCount = 7;

	private float cronometroNovaBacteria;
	private float lifeCronometer = 0;

	private List<GameObject> cemetery;
	private List<ShieldBacteria> defenserBacterias;

	public List<ShieldBacteria> getGameObjects() {
		return defenserBacterias;
	}

	private GameSceneView gameSceneView;

	public ShieldBacteriaManager(List<GameObject> cemetery,
			GameSceneView gameSceneView) {

		this.defenserBacterias = new ArrayList<ShieldBacteria>();

		this.cemetery = cemetery;

		this.gameSceneView = gameSceneView;

		this.nextInterval = NextInterval(minInterval, maxInterval);

		Log.i(TAG, "Shield Bacteria Manager - instanciado");		
	}

	@Override
	public void Update(double deltaTime) {
		
		if (cronometroNovaBacteria >= nextInterval) {

			ShieldBacteria shieldBacteria = new ShieldBacteria(defenserBacterias,
					cemetery, this.gameSceneView);

			cronometroNovaBacteria = 0;

			this.nextInterval = NextInterval(this.minInterval, this.maxInterval);

		} else {
			cronometroNovaBacteria += deltaTime;
		}

		this.lifeCronometer += deltaTime;
		if (lifeCronometer > this.intervalUpDificult && this.upDificultOn) {
			this.minInterval -= this.difucultIncrease;
			if (minInterval < 0) {
				minInterval = 0;
			}
			this.maxInterval -= this.difucultIncrease;
			Log.i(TAG,
					String.valueOf(this.minInterval) + ", "
							+ String.valueOf(this.maxInterval));
			contadorDificuldade++;
			if (contadorDificuldade >= this.dificultCount) {
				upDificultOn = false;
			}

			lifeCronometer = 0;
		}
		// ============================================================
		// Destroy elements no more used
		if (!cemetery.isEmpty()) {

			for (GameObject gb : cemetery) {
				gb.Destroy();
				gb = null;
			}
		}
		cemetery.clear();
		// ===========================================================

		if (!defenserBacterias.isEmpty()) {
			for (GameObject go : defenserBacterias) {
				go.Update(TimeManager.getInstance().getDeltaTime());
			}
		}
	}

	// =======================================================================	// DRAW

	@Override
	public void Draw(Canvas canvas) {
		if (!defenserBacterias.isEmpty()) {
			for (GameObject go : defenserBacterias) {
				go.Draw(canvas);

			}
		}
	}

	// ========================================================================
	// TOUCH
	@Override
	public boolean onTouch(MotionEvent event) {

		try {
			if (!defenserBacterias.isEmpty()) {
				for (ShieldBacteria bacteria : defenserBacterias) {
					bacteria.onTouch(event);
				}
			}
		} catch (Exception e) {
			Log.i(TAG, "erro ao toque");
		}
		
		return true;

	}

	private float NextInterval(float min, float max) {
		float interval = min + MinhasCoisas.getRandom().nextFloat()
				* (max - min);
		return interval;
	}

}
